package com.gc.common.util;

import com.gc.common.constants.FileConstants;
import com.gc.common.constants.SysConstants;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
public class MyUtil {

    /**
     * 用于授权时 使用 , 拆分权限列表字符串
     * @param s
     * @return
     */
    public List<Integer> split(String s){
        if(StringUtils.isEmpty(s)) return null;
        List<Integer> list = new ArrayList<>();
        s = s.trim();
        if(s.length() == 1) {
            list.add(Integer.valueOf(s));
            return list;
        }
        String[] split = s.split(",");
        for (int i = 0; i < split.length; i++) {
            list.add(Integer.valueOf(split[i]));
        }
        return list;
    }

    /**
     * base64转图片 , 并存本地
     * @param imgStr
     * @return
     */
    public static String Base64ToImage(String imgStr,String localUrl) { // 对字节数组字符串进行Base64解码并生成图片
        String dataPrix = ""; //base64格式前头
        String data = "";//实体部分数据
        if(imgStr==null||"".equals(imgStr)){
            return null;
        } else if(imgStr.contains(FileConstants.VOR_IMG_VIRTUAL_URL)){
            return imgStr;
        }else {
            String [] d = imgStr.split("base64,");//将字符串分成数组
            if(d != null && d.length == 2){
                dataPrix = d[0];
                data = d[1];
            }else {
                return null;
            }
        }
        String suffix = "";//图片后缀，用以识别哪种格式数据
        //编码的jpeg图片数据
        if("data:image/jpeg;".equalsIgnoreCase(dataPrix) || "data:image/jpg;".equalsIgnoreCase(dataPrix)){
            suffix = ".jpg";
        }else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){
            //编码的icon图片数据
            suffix = ".ico";
        }else if("data:image/gif;".equalsIgnoreCase(dataPrix)){
            //编码的gif图片数据
            suffix = ".gif";
        }else if("data:image/png;".equalsIgnoreCase(dataPrix)){
            //编码的png图片数据
            suffix = ".png";
        }else {
            return null;
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String tempFileName=uuid+suffix;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(data);
            for(int i=0;i<b.length;++i) {
                if(b[i]<0) {
                    //调整异常数据
                    b[i]+=256;
                }
            }
            OutputStream out = new FileOutputStream(localUrl + tempFileName);
            out.write(b);
            out.flush();
            out.close();
            if(localUrl.contains("images")){
                tempFileName = FileConstants.VOR_IMG_VIRTUAL_URL + tempFileName;
            }
            return tempFileName;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getPingYin(String str){
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        char[] hanYuArr  = str.toCharArray();
        StringBuilder pinYin = new StringBuilder();

        try {
            for (int i = 0, len = hanYuArr.length; i < len; i++) {
                //匹配是否是汉字
                if (Character.toString(hanYuArr[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    //如果是多音字，返回多个拼音，这里只取第一个
                    String[] pys = PinyinHelper.toHanyuPinyinStringArray(hanYuArr[i], format);
                    pinYin.append(pys[0].charAt(0));
                } else {
                    pinYin.append(hanYuArr[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return pinYin.toString();
    }
}
